/* -*- mode:c++ -*- ********************************************************
 * file:        BurstApplLayer_A.cc
 *
 * author:      Daniel Willkomm
 *
 * copyright:   (C) 2004 Telecommunication Networks Group (TKN) at
 *              Technische Universitaet Berlin, Germany.
 *
 *              This program is free software; you can redistribute it
 *              and/or modify it under the terms of the GNU General Public
 *              License as published by the Free Software Foundation; either
 *              version 2 of the License, or (at your option) any later
 *              version.
 *              For further information see file COPYING
 *              in the top level directory
 ***************************************************************************
 * part of:     framework implementation developed by tkn
 * description: application layer like the TestApplLayer_A but sends burst of
 *               messages
 **************************************************************************/


#include "BurstApplLayer_A.h"

Define_Module(BurstApplLayer_A);

// do some initialization
void BurstApplLayer_A::initialize(int stage)
{
	TestApplLayer::initialize(stage);
	ip = par("ip").longValue();

	reTXInterval = hasPar("reTXInterval") ? par("reTXInterval").doubleValue() : .05;

	shouldTx = hasPar("shouldTx") ? par("shouldTx").boolValue() : false;

	ev << "Re-transmission interval = " << reTXInterval << "\n";
	if(stage==0){
		burstSize = hasPar("burstSize") ? par("burstSize") : 1;
	}
}


void BurstApplLayer_A::handleSelfMsg(cMessage *msg)
{
	switch(msg->getKind())
	{
	case SEND_BROADCAST_TIMER:
		//for(int i=0; i<burstSize; i++) {
			//sendBroadcast();
			//sendUnicast(); //sendBroadcast();
		//}
		ev << "My host IP = " << ip << "\n\n\n";

		if (shouldTx)
		{
			for (int i = 0; i < burstSize ; i++) {
				sendBroadcast();
			}
			//scheduleAt(simTime() + reTXInterval , delayTimer);
			scheduleAt(simTime() + dblrand() * reTXInterval , delayTimer);
		}

		break;
	default:
		EV <<" Unknown self message! -> delete, msg kind: " << msg->getKind() << endl;
	}
}

/**
 * There are two kinds of messages that can arrive at this module: The
 * first (kind = BROADCAST_MESSAGE) is a broadcast packet from a
 * neighbor node to which we have to send a reply. The second (kind =
 * BROADCAST_REPLY_MESSAGE) is a reply to a broadcast packet that we
 * have send and just causes some output before it is deleted
 **/
//void TestApplLayer_A::handleLowerMsg( cMessage* msg )
void BurstApplLayer_A::handleLowerMsg( cMessage* msg )
{
    ApplPkt *m;
    switch( msg->getKind() ){
    case BROADCAST_MESSAGE:
        m = static_cast<ApplPkt *>(msg);
        coreEV << "Received a broadcast packet from host["<<m->getSrcAddr()<<"] -> NOT !!!! sending reply\n";
        //sendReply(m);
        delete msg;
        break;
    case BROADCAST_REPLY_MESSAGE:
        m = static_cast<ApplPkt *>(msg);
        coreEV << "Received reply from host["<<m->getSrcAddr()<<"]; delete msg\n";
        delete msg;
	break;
    default:
	EV <<"Error! got packet with unknown kind: " << msg->getKind()<<endl;
        delete msg;
    }
}

